package luafrontend.symbol;

import java.util.ArrayList;
import java.util.HashMap;

public class SymbolTable<K, V> {
	private final ArrayList<HashMap<K, V>> scopes = new ArrayList<HashMap<K, V>>();
	private int counter;
	
	private HashMap<K, V> getScope() {
		return scopes.get(counter - 1);
	}
	
	public void enter() {
		counter += 1;
		
		if (counter > scopes.size()) {
			scopes.add(new HashMap<K, V>());
		}
	}
	
	public void leave() {
		getScope().clear();
		
		counter -= 1;
	}
	
	public void put(K key, V value) {
		getScope().put(key, value);
	}
	
	public V get(K key) {
		return getScope().get(key);
	}
	
	public void remove(K key) {
		getScope().remove(key);
	}
}
